package com.lv.controller;

import com.lv.data.Result;
import com.lv.enums.ResultEnum;
import com.lv.pojo.TbAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @RequestMapping("login")
    public String login() {
        return "login";
    }

    @RequestMapping("aaaa")
    @ResponseBody
    public Result aaaa(TbAdmin a){
        //创建subject对象
        Subject subject = SecurityUtils.getSubject();
        //将表单提交的 账号和密码 封装成shiro的 token
        UsernamePasswordToken token = new UsernamePasswordToken(a.getUname(), a.getPwd());
        //执行登录逻辑
        try {
            subject.login(token);
            return Result.OK();
        } catch (UnknownAccountException ae) {
            return Result.response(ResultEnum.NAME_NO);
        } catch (IncorrectCredentialsException ice) {
            return Result.response(ResultEnum.PWD_ER);

        }
    }
}
